<?php

// +----------------------------------------------
// | Name： 德瑞骏发 接口api 首页
// +----------------------------------------------
// | Version： V5.0
// +----------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +----------------------------------------------
// | Time: 2021-05-06 10:05
// +----------------------------------------------

declare(strict_types=1);

namespace app\api\controller\v1;

use app\common\common\IndexController;
use app\common\model\Artice as ArticeModel;
use app\common\model\Banner as BannerModel;
use app\common\model\Goods as GoodsModel;
use app\common\model\User as UserModel;
use app\common\controller\Wechatclass;
use app\common\model\GoodsCate as GoodsCateModel;

/**
 * @xk 首页api
 */
class Index extends IndexController
{
    /**
     * @xk 首页数据获取
     *
     * @method getIndexAll
     * @param  BannerModel    $bannerModel               [description]
     * @param  ArticeModel    $articeModel               [description]
     * @param  GoodsModel     $goodsModel                [description]
     * @param  GoodsCateModel $GoodsCateModel            [description]
     * @return [type]         [description]
     * @Time   2021-10-19
     * @City   青岛
     * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
     */
    public function getIndexAll(BannerModel $bannerModel, ArticeModel $articeModel, GoodsModel $goodsModel, GoodsCateModel $goodsCateModel, UserModel $userModel)
    {
        $param = request()->param();
        $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
        // 获取商城公告
        $artice_condition = [];
        $artice_condition[] = ['a.status', '=', 1];
        $artice_condition[] = ['a.cate_str', 'like', '%,1,%'];
        $artice_list = $articeModel
              ->alias('a')
              ->where($artice_condition)
              ->field('a.`artice_id`,a.`title`,a.`linkurl`')
              ->order('order_id desc')
              ->select()
              ->toArray();
        // 获取广告图
        $banner_condition = [];
        $banner_condition[] = ['b.banner_type_id', '=', 1]; // 首页广告图
        $banner_condition[] = ['b.status', '=', 1];
        $banner_list = $bannerModel
                        ->alias('b')
                        ->where($banner_condition)
                        ->field('b.`banner_id`,b.`banner_name`,b.`show_type`,b.`linkurl`,b.`fileurl`,b.`target`,b.`show_pic_url`')
                        ->order('order_id desc')
                        ->withAttr('fileurl', function ($value, $data) {
                            if ($value) {
                                return getShowUrl($value);
                            }
                        })
                        ->withAttr('show_pic_url', function ($value, $data) {
                            if ($value) {
                                return getShowUrl($value);
                            }
                        })
                        ->select()
                        ->toArray();
        // 获取产品列表
        $goods_cate_condition = [];
        $goods_cate_condition[] = ['status', '=', 1];
        $new_list = [];
        $new_list[] = ['goods_cate_id'=>'tj','goods_cate_name'=>'精选'];
        $list = $goodsCateModel
                ->where($goods_cate_condition)
                ->field('`goods_cate_id`,`goods_cate_name`')
                ->order(['order_id asc'])
                ->select()
                ->toArray();
        $list = array_merge($new_list, $list);
        if ($list) {
            foreach ($list as $key => $value) {
                $goods_condition = [];
                $goods_condition[] = ['is_onsell','=',1];
                if ($value['goods_cate_id'] != 'tj') {
                    $goods_condition[] = ['goods_cate_str', 'like', '%,'.$value['goods_cate_id'].',%'];
                }else{
                  $goods_condition[] = ['is_tj','=',1];
                }
                $list[$key]['goods_list'] = $goodsModel
            ->where($goods_condition)
            ->field('`goods_id`,`goods_name`,`picurl`,`selling_price`,`keywords`,`vip_buy_bili`,`is_onsell` as is_buy')
            ->withAttr('is_buy', function ($value, $data) {
                return  0;
            })
            ->withAttr('picurl', function ($value, $data) {
                return getShowUrl($value);
            })
            ->order(['order_id asc','create_time desc'])
            ->select()
            ->toArray();
            }
        }

        // 获取用户信息 判断用户首购/复购

        $is_buy = $userModel->where([['user_sn','=',$user_sn]])->value('`is_buy`');

        if ($list && $is_buy == 1) {
            foreach ($list as $key => $value) {
                if ($value['goods_list']) {
                    foreach ($value['goods_list'] as $k => $val) {
                        $list[$key]['goods_list'][$k]['selling_price'] = sprintf('%.2f', ((float)$val['selling_price']*(float)$val['vip_buy_bili'])); // 最终获得折扣价格
                        $list[$key]['goods_list'][$k]['is_buy'] = 1;
                    }
                }
            }
        }

        $code = 200;
        $msg = '请求成功';
        $result_code = 200;
        $result_msg = '加载成功';
        $resultData = [];
        $resultData['banner_list'] = $banner_list;
        $resultData['artice_list'] = $artice_list;
        $resultData['goods_cate_list'] = $list;
        $resultData['web_logo'] = config('web_config.web_logo');
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 获取关于我们
     * @method                                      getAboutUs
     * @return                                      [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe                             <zhonghuaxinxing@sina.cn>
     * @City：                                       青岛
     * @Time：2022-02-22Tfalse15:false18:false35+080
     */
    public function getAboutUs()
    {
        $code = 200;
        $msg = '请求成功';
        $result_code = 200;
        $result_msg = '加载成功';
        $resultData = [];
        $resultData['logo_picurl'] = config('web_config.logo_picurl');
        $resultData['version'] = '2.0.0';
        $resultData['yartice_id'] = 14;
        $resultData['yiartice_id'] = 15;
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 转化链接
     *
     * @method getShowUrl
     * @return [type]     [description]
     * @Time   2021-10-08
     * @City   青岛
     * @Author xkyinzhe   <zhonghuaxinxing@sina.cn>
     */
    public function getShowUrl()
    {
        $param = request()->param();
        $url = isset($param['url']) ? $param['url'] : '';
        $this->result(200, '加载成功', 200, '转化成功', getShowUrl($url));
    }
    /**
     * @xk 微信分享接口
     *
     * @method getWechatInfo
     * @return [type]        [description]
     * @Time   2021-10-29
     * @City   青岛
     * @Author xkyinzhe      <zhonghuaxinxing@sina.cn>
     */
    public function getWechatInfo()
    {
        $param = request()->param();
        $url = 'https://'.$_SERVER['HTTP_HOST'].'/';
        $resultData = [];
        $resultData['timestamp'] = (string)time();
        $resultData['url'] = $url;
        $options = [
          'appid' => config('core_config.wechat_config.app_id'), // 填写高级调用功能的app id
          'appsecret' => config('core_config.wechat_config.app_script'), // 填写高级调用功能的密钥
        ];
        $weObj = new Wechatclass($options);
        $resultData['noncestr'] = $weObj->generateNonceStr();
        $resultData['jsapi_ticket'] = $weObj->getJsApiTicket();
        $resultData['signature'] = $weObj->getSignature($resultData);
        $resultData['nonceStr'] =   $resultData['noncestr'];
        $resultData['appId'] = config('core_config.wechat_config.app_id');
        $this->result(200, '加载成功', 200, '请求成功', $resultData);
    }
    public function getVersion(){
      $resultData['app_version'] = config('web_config.app_version');
      $resultData['update_content'] = chanageContent(config('web_config.update_content'));
      $resultData['android_link'] = config('web_config.android_link');
      $resultData['apple_id'] = config('web_config.apple_id');
      $resultData['is_other_login'] = 1;
      $this->result(200, '加载成功', 200, '请求成功', $resultData);
    }
}
